public class Demo4_23 {
    //统计字符串中的单词个数，这里的单词指的是连续的不是空格的字符
    public static int countSegments(String s) {
        if (s.length() == 0) {
            return 0;
        }
        String[] ret = s.split(" ");
        int count = 0;
        for (String x : ret) {
            if (x.length() != 0) {
                count++;
            }

        }
        return count;
    }

    public static void main(String[] args) {
        String str = "Hello, My name is Mike!";
        System.out.println(countSegments(str));
    }
}
